package com.lxw.manage.course.service;


import com.lxw.framework.domain.course.CourseBase;
import com.lxw.framework.domain.course.Teachplan;
import com.lxw.framework.domain.course.ext.TeachplanNode;
import com.lxw.framework.exception.ExceptionCast;
import com.lxw.framework.model.response.*;
import com.lxw.manage.course.dao.CourseBaseRepository;
import com.lxw.manage.course.dao.TeachplanMapper;
import com.lxw.manage.course.dao.TeachplanRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 *
 */
@Service
@Slf4j
public class TeachPlanService {

    @Autowired
    private TeachplanMapper teachplanMapper;

    @Autowired
    TeachplanRepository teachplanRepository;

    @Autowired
    private CourseBaseRepository courseBaseRepository;

    public TeachplanNode findTeanplansByCourseId(String courseId){
        TeachplanNode teachplanNode = teachplanMapper.selectList(courseId);
        return teachplanNode;
    }

    @Transactional
    public ResponseResult addTeachPlan(Teachplan teachplan) {
        if (teachplan == null){
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        if(StringUtils.isEmpty(teachplan.getCourseid())) {
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        if(StringUtils.isEmpty(teachplan.getPname())) {
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        String parentId = teachplan.getParentid();
        //如果父节点为空,判断一级节点是否存在,不存在,先添加一级节点,再添加二级节点.
        if (StringUtils.isEmpty(parentId)){
            parentId = getParentId(teachplan.getCourseid());
            teachplan.setParentid(parentId);
        }

        //获取父节点信息
        Optional<Teachplan> optional = teachplanRepository.findById(parentId);
        if (!optional.isPresent()){
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        Teachplan teachplanParent = optional.get();
        //如果父节点非空,

        if (teachplanParent.getGrade().equals("1")){
            teachplan.setGrade("2");
        }else {
            teachplan.setGrade("3");
        }

        teachplanRepository.save(teachplan);
        ResponseResult responseResult = new ResponseResult(CommonCode.SUCCESS);
        return  responseResult;
    }

    //根据课程id,parentId=0查询课程计划信息,如果查到信息,返回父节点id;
    // 否则.先添加一级节点.添加成功,返回父节点id
    private String  getParentId(String coureId){

        List<Teachplan> list = teachplanRepository.findByCourseidAndParentid(coureId, "0");

        if (list == null || list.size()<=0){ //说明数据库中没有该课程计划的一级节点

            Optional<CourseBase> op = courseBaseRepository.findById(coureId);
            if (!op.isPresent()){
                ExceptionCast.cast(CommonCode.INIT_PARAM);
            }
            CourseBase courseBase = op.get();
            //添加课程计划一级节点
            Teachplan teachplan = new Teachplan();
            teachplan.setPname(courseBase.getName());
            teachplan.setParentid("0");
            teachplan.setGrade("1");
            teachplan.setCourseid(coureId);
            teachplanRepository.save(teachplan);
            return teachplan.getId();
        }else{
            Teachplan teachplan = list.get(0);
            return  teachplan.getId();
        }
    }
}
